package cfgs.gestudent.dao.curs;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

import javax.swing.JOptionPane;

import cfgs.gestudent.dao.connexio.ConnexioDB;
import cfgs.gestudent.preferencies.ConfiguracionsIdiomes;

public class EliminarAssistenciaAlumne {
	
	private ConfiguracionsIdiomes idioma = new ConfiguracionsIdiomes();
	private ResourceBundle rb = idioma.getIdioma();
	
	public boolean eliminarRegistre(int idAlumne, int idImpartirAssign, String fecha, String hora){
		boolean seguir = false;
		
		Statement st = null;
		Connection connexio = ConnexioDB.getConnexio();
		int confirmacio;
		try{
			connexio.setAutoCommit(false);
			String delete = "DELETE FROM ASSISTENCIA WHERE Id_Alumne="+idAlumne+" AND Id_Impartir_Assignatura="+idImpartirAssign+" AND Fecha=STR_TO_DATE('"+fecha+"', '%d-%m-%Y') AND Hora='"+hora+"'";
			System.out.println(delete);
			confirmacio = JOptionPane.showConfirmDialog(null, rb.getString("confirmEliminar"), rb.getString("titolConfirmElimin"), JOptionPane.YES_NO_OPTION);
			if(JOptionPane.OK_OPTION == confirmacio){
				st = connexio.createStatement();
				st.executeUpdate(delete);
				connexio.commit();
			}else if(JOptionPane.CANCEL_OPTION == confirmacio || JOptionPane.CLOSED_OPTION == confirmacio){
				connexio.rollback();
			}
		}catch(SQLException e){
			e.printStackTrace();
			JOptionPane
					.showMessageDialog(null,
							rb.getString("missatgeErrorEliminar")
									+ "\n " + e,
							rb.getString("titolErrorEliminar"),
							JOptionPane.ERROR_MESSAGE);
		}try {
			connexio.close();
			st.close();
		} catch (SQLException e) {
			e.printStackTrace();
			JOptionPane
					.showMessageDialog(null,
							rb.getString("missatgeErrorTancarConnexio")
									+ "\n " + e,
							rb.getString("titolErrorTancarConnexio"),
							JOptionPane.ERROR_MESSAGE);
		}
		
		return seguir;
	}

}
